package com.hd.sup.core.task;

import com.alibaba.fastjson.JSON;
import com.hd.common.constant.BizConstant;
import com.hd.common.utils.DateUtil;
import com.hd.common.utils.GenUtil;
import com.hd.sup.config.rabbitmq.producer.RabbitProducer;
import com.hd.sup.config.redis.prefix.CacheKey;
import com.hd.sup.config.redis.utils.JedisUtil;
import com.hd.sup.modules.alarm.model.AlarmDailyStatis;
import com.hd.sup.modules.alarm.service.IAlarmDailyStatisService;
import com.hd.sup.modules.vehicle.model.VehicleAlarmDailyStatis;
import com.hd.sup.modules.vehicle.model.VehicleDailyStatis;
import com.hd.sup.modules.vehicle.model.VehicleEndPos;
import com.hd.sup.modules.vehicle.service.IVehicleAlarmDailyStatisService;
import com.hd.sup.modules.vehicle.service.IVehicleDailyStatisService;
import com.hd.sup.modules.vehicle.service.IVehicleEndPosService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;


/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title ScheduledTasks.java
 * @Description SpringBoot定时任务工具类
 * 各定时任务执行频率:
 * 末次位置信息更新 8min
 * 静态车辆数据同步 20min
 * 自动统计 sp_alarm_daily_statis              3min
 * 自动统计 sp_vehicle_alarm_daily_statis      4min
 * 自动统计 sp_vehicle_daily_statis            5min
 * @Package com.hd.sup.core.task
 * @date 2021/1/14 1:54 下午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@Component
public class ScheduledTasks {

    @Resource
    private IVehicleEndPosService vehicleEndPosService;

    @Resource
    private IAlarmDailyStatisService alarmDailyStatisService;

    @Resource
    private IVehicleAlarmDailyStatisService vehicleAlarmDailyStatisService;

    @Resource
    private IVehicleDailyStatisService vehicleDailyStatisService;

    @Resource
    private JedisUtil jedisUtil;

    @Resource
    private RabbitProducer rabbitProducer;

    /**
     * @return void
     * @author chentl
     * @description 15min 执行一次 将缓存中定位信息刷入 sp_vehicle_end_pos表
     * @date 2021/1/8 12:35 下午
     * @params []
     */
    @Scheduled(cron = "0 */3 * * * ? ")
    public void vehicleEndPosRefresh() {
        log.info("[schedule] 【末次位置信息更新】定时任务执行");
        //查询所有末次信息
        Set<String> keyList = (Set<String>) jedisUtil.getKeys(CacheKey.END_POSITION_KEY);

        List<String> resultList = jedisUtil.mget(keyList.toArray(new String[]{}));
        if (CollectionUtils.isEmpty(resultList)) return;

        VehicleEndPos ve;
        for (String s : resultList) {
            if (s == null) continue;
            ve = JSON.parseObject(s, VehicleEndPos.class);
            if (!StringUtils.isAnyEmpty(ve.getPlatNo(), ve.getPlatNoColor()))
                vehicleEndPosService.refreshVehicleEndPos(ve);
        }


        log.info("[schedule] 【末次位置信息更新】定时任务结束");
    }

    /**
     * @return void
     * @author chentl
     * @description 20min 同步一次静态车辆数据
     * @date 2021/1/13 5:21 下午
     * @params []
     */
//    @Scheduled(cron = "0 */40 * * * ? ")
    public void syncStaticData() {
        log.info("[schedule] 【静态车辆数据同步】定时任务执行");
        List<VehicleEndPos> result = vehicleEndPosService.getUnCompletionVehicle();
        if (CollectionUtils.isEmpty(result)) return;

        //开始查询 尚未同步的静态数据的车辆信息 发往同步队列
        result.forEach(one -> rabbitProducer.sendToStaticDataSyncQueue(JSON.toJSONString(one)));
        log.info("[schedule] 【静态车辆数据同步】定时任务执行");
    }


    /**
     * @return void
     * @author chentl
     * @description 30min sp_alarm_daily_statis 统计
     * @date 2021/1/9 10:10 上午
     * @params []
     */
    @Scheduled(cron = "0 */6 * * * ?")
    public void alarmDetailDailyStatistics() {
        Date now = new Date();
        String currentDate = DateUtil.formatDate(now, BizConstant.Common.DATE_FORMAT_DAY.key());

        log.info("[schedule] 【自动统计】 sp_alarm_daily_statis 定时任务执行,统计日期:{}", currentDate);

        List<AlarmDailyStatis> record = alarmDailyStatisService.getTodayStatisData(currentDate);
        if (CollectionUtils.isEmpty(record)) return;

        for (AlarmDailyStatis one : record) {
            one.setId(GenUtil.newId());
            one.setStatisDate(now);
            if (!StringUtils.isAnyEmpty(one.getEnterpriseNo(), one.getAlarmType()))
                alarmDailyStatisService.refreshAlarmDailyStatis(one);
        }

        log.info("[schedule] 【自动统计】 sp_alarm_daily_statis 定时任务结束");
    }

    /**
     * @return void
     * @author chentl
     * @description 20min sp_vehicle_daily_statis 统计
     * @date 2021/1/11 10:20 上午
     * @params []
     */
    @Scheduled(cron = "0 */5 * * * ?")
    public void alarmVehicleDailyStatistics() {
        Date now = new Date();
        String currentDate = DateUtil.formatDate(now, BizConstant.Common.DATE_FORMAT_DAY.key());
        log.info("[schedule] 【自动统计】 sp_vehicle_alarm_daily_statis 定时任务执行,统计日期:{}", currentDate);

        List<VehicleAlarmDailyStatis> record = vehicleAlarmDailyStatisService.getTodayStatisData(currentDate);
        if (CollectionUtils.isEmpty(record)) return;

        for (VehicleAlarmDailyStatis one : record) {
            one.setId(GenUtil.newId());
            one.setStatisDate(now);
            if (!StringUtils.isAnyEmpty(one.getAlarmType(), one.getTransType()))
                vehicleAlarmDailyStatisService.refreshVehicleAlarmDailyStatis(one);
        }

        log.info("[schedule] 【自动统计】 sp_vehicle_alarm_daily_statis 定时任务结束");
    }

    /**
     * @return void
     * @author chentl
     * @description 15min sp_vehicle_daily_statis 统计
     * @date 2021/1/11 1:44 下午
     * @params []
     */
    @Scheduled(cron = "0 */4 * * * ?")
    public void vehicleDailyStatistics() {
        Date now = new Date();
        String currentDate = DateUtil.formatDate(now, BizConstant.Common.DATE_FORMAT_DAY.key());

        log.info("[schedule] 【自动统计】 sp_vehicle_daily_statis 定时任务执行,统计日期:{}", currentDate);

        List<VehicleDailyStatis> record = vehicleDailyStatisService.getTodayStatisData(currentDate);
        if (CollectionUtils.isEmpty(record)) return;

        for (VehicleDailyStatis one : record) {
            one.setId(GenUtil.newId());
            one.setStatisDate(now);
            if (!StringUtils.isAnyEmpty(one.getEnterpriseNo(), one.getTransType()))
                vehicleDailyStatisService.refreshVehicleDailyStatis(one);
        }

        log.info("[schedule] 【自动统计】 sp_vehicle_daily_statis 定时任务结束");
    }


}
